<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="space-between">
          <el-col>江苏传智播客教育科技股份有限公司</el-col>
          <el-col :span="4">
            <span>负责人</span>
            <el-dropdown>
              <span class="el-dropdown-link">
                下拉菜单<i class="el-icon-arrow-down el-icon--right" />
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>添加子部门</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <tree-tools slot-scope="{ data }" :tree-node="data" @delDepartment="getDepartments" @addDepart="handleAddDepart" @editDepart="handleEditDepart" />
        </el-tree>
      </el-card>
    </div>
    <add-dept v-if="showDialog" ref="addDept" :show-dialog="showDialog" :node="node" @close="showDialog=false" @add="add" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      departs: [],
      company: { }, // 就是头部的数据结构
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false,
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人' }
      this.departs = tranListToTreeData(result.depts, '') // 需要将其转化成树形结构
      console.log(result)
    },
    handleAddDepart(node) {
      this.showDialog = true
      this.node = node
      console.log(this.node)
    },
    add() {
      this.showDialog = false
      this.getDepartments()
    },
    handleEditDepart(node) {
      console.log(node)
      this.node = node
      this.showDialog = true
      // 主动的调用一下add-depart.vue这个组件的getData方法
      // 父调用子的方法的一种思路
      // console.log(this.$refs.addDept)
      // this.$refs.addDept.getData(node.id)
      this.$nextTick(() => {
        this.$refs.addDept.getData(node.id)
      })
    }
  }
}

</script>

<style scoped>
.el-dropdown{
  margin-left:20px;
}
.dashboard-container{
  font-size:14px;
}
.el-tree{
  margin-top:16px;
}
</style>>
